//
//  SinaTabBar.swift
//  SinaWeiBo
//
//  Created by 刘世瑛 on 16/1/19.
//  Copyright © 2016年 刘世瑛. All rights reserved.
//

import UIKit

class SinaTabBar: UITabBar {

    
    
    
    
    //1 添加button
    override init(frame: CGRect) {
    
        super.init(frame: frame)
        setUpUI()
    }
    

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //2修改frame
    
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
       var index = 0
        let w = bounds.width/5
        composeButton.center = CGPointMake(bounds.width/2.0, bounds.height/2.0)
        
        for bt in subviews
        {
            if bt.isKindOfClass(NSClassFromString("UITabBarButton")!)
            {
             
                bt.frame = CGRectMake(w * CGFloat(index), 0, w, bounds.height)
                
            
                index =  index == 1 ? index + 2 : index + 1
                
            }
            
        }
      
    }
    
    
    
    
    private func setUpUI(){
        
        addSubview(composeButton)
        
    }
    /**
    *  懒加载button
    */

    lazy var composeButton:UIButton = {
        let button = UIButton(type: .Custom)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)

        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        
        button.sizeToFit()
        
        return button
    }()
}
